package cn.ffcs.uec.outStaff.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffTramsgEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.model.UecOutStaffTramsg;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecOutStaffTramsgEditComposer extends BasePortletComposer {

    private static final long serialVersionUID = -6096343177536382090L;

    private UecOutStaffTramsgEditBean bean = new UecOutStaffTramsgEditBean();

    private String opType;

    @Resource
    private IdentityCardConfigManager identityCardConfigManager;

    private UecOutStaffTramsg uecOutStaffTramsg;

    private UecOutStaffInfo uecOutStaffInfo;


    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        Components.wireVariables(comp, bean);
    }

    /**
     * window初始化.
     * 
     * @throws Exception
     *             异常
     */
    public void onCreate$uecOutStaffTramsgEditWin() throws Exception {
        bindCombobox();
        opType = (String) arg.get("opType");
        uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
        if (opType.equals("add")) {
            bean.getUecOutStaffTramsgEditWin().setTitle("外包人员培训信息新增");
        } else if (opType.equals("edit")) {
            bean.getUecOutStaffTramsgEditWin().setTitle("外包人员培训信息修改");
            strToDate();
        } else if (opType.equals("view")) {
            bean.getUecOutStaffTramsgEditWin().setTitle("外包人员培训信息查看");
            strToDate();
            PubUtil.disableComponent(bean.getUecOutStaffTramsgEditWin());
        }
    }

    /**
     * 保存.
     * 
     * @throws Exception
     */
    public void onOk() throws Exception {
        String msg = checkUacStaffTramsgData();
        if (msg != null) {
            Messagebox.show(msg);
            return;
        }
        uecOutStaffTramsg = (UecOutStaffTramsg) arg.get("uecOutStaffTramsg");
        UecOutStaffTramsg uecOutStaffTramsgObj = UecOutStaffTramsg.newInstance();
        if (uecOutStaffTramsg != null) {
            PubUtil.fillPoFromEditorBean(uecOutStaffTramsg, bean);
        } else {
            uecOutStaffTramsg = uecOutStaffTramsgObj;
            uecOutStaffTramsg.setOutStaffId(uecOutStaffInfo.getId());
            PubUtil.fillPoFromEditorBean(uecOutStaffTramsg, bean);
        }
        if (opType.equals("add")) {
            // 保存员工
            String strBeginDate = DateUtil.dateToStr(this.bean.getBegindate().getValue());
            uecOutStaffTramsg.setBegindate(strBeginDate);
            String strEndDate = DateUtil.dateToStr(this.bean.getEnddate().getValue());
            uecOutStaffTramsg.setEnddate(strEndDate);
            String wbBeginDate = DateUtil.dateToStr(this.bean.getTraValidity().getValue());
            uecOutStaffTramsg.setTraValidity(wbBeginDate);
            String wbEndDate = DateUtil.dateToStr(this.bean.getTraSignDate().getValue());
            uecOutStaffTramsg.setTraSignDate(wbEndDate);
            uecOutStaffTramsg.setStatusCd("1000");
            uecOutStaffTramsg.addOnly();
            Events.postEvent("onOK", this.self, uecOutStaffTramsg);
            onCancel();
            Messagebox.show("新增成功！");
        } else if (opType.equals("edit")) {
            String strBeginDate = DateUtil.dateToStr(this.bean.getBegindate().getValue());
            uecOutStaffTramsg.setBegindate(strBeginDate);
            String strEndDate = DateUtil.dateToStr(this.bean.getEnddate().getValue());
            uecOutStaffTramsg.setEnddate(strEndDate);
            String wbBeginDate = DateUtil.dateToStr(this.bean.getTraValidity().getValue());
            uecOutStaffTramsg.setTraValidity(wbBeginDate);
            String wbEndDate = DateUtil.dateToStr(this.bean.getTraSignDate().getValue());
            uecOutStaffTramsg.setTraSignDate(wbEndDate);
            uecOutStaffTramsg.setStatusCd("1000");
            uecOutStaffTramsg.updateOnly();
            Events.postEvent("onOK", this.self, uecOutStaffTramsg);
            onCancel();
            Messagebox.show("修改成功！");
        }
        onCancel();
    }

    /**
     * 取消.
     */
    public void onCancel() {
        bean.getUecOutStaffTramsgEditWin().onClose();
    }

    private String checkUacStaffTramsgData() {
        return null;
    }

    /**
     * 字符串转date
     */
    public void strToDate() {
        if (arg.get("uecOutStaffTramsg") != null) {
            uecOutStaffTramsg = (UecOutStaffTramsg) arg.get("uecOutStaffTramsg");
            PubUtil.fillBeanFromPo(uecOutStaffTramsg, bean);
            if (!StrUtil.isEmpty(uecOutStaffTramsg.getBegindate())) {
                bean.getBegindate().setValue(DateUtil.patternStrToDate(uecOutStaffTramsg.getBegindate()));
            }
            if (!StrUtil.isEmpty(uecOutStaffTramsg.getEnddate())) {
                bean.getEnddate().setValue(DateUtil.patternStrToDate(uecOutStaffTramsg.getEnddate()));
            }
            if (!StrUtil.isEmpty(uecOutStaffTramsg.getTraValidity())) {
                bean.getTraValidity().setValue(DateUtil.patternStrToDate(uecOutStaffTramsg.getTraValidity()));
            }
            if (!StrUtil.isEmpty(uecOutStaffTramsg.getTraSignDate())) {
                bean.getTraSignDate().setValue(DateUtil.patternStrToDate(uecOutStaffTramsg.getTraSignDate()));
            }
        }
    }

 

    /**
     * 绑定下拉框 绑定combobox
     */
    private void bindCombobox() {
        // 培训类别
        ListboxUtils.rendererForEdit(bean.getTraClass(), NodeVoUtil.getDictionaries(UecConstants.TRAIN_TYPE));
        // 培训方式
        ListboxUtils.rendererForEdit(bean.getTraMode(), NodeVoUtil.getDictionaries(UecConstants.TRAIN_MODE));
        // 培训地点
        ListboxUtils.rendererForEdit(bean.getTraLocation(), NodeVoUtil.getDictionaries(UecConstants.TRA_LOCATION));
        // 参训方式
        ListboxUtils.rendererForEdit(bean.getTraMethod(), NodeVoUtil.getDictionaries(UecConstants.TRA_METHOD));
    }
}
